<?xml version="1.0"?>
<!--
lib_pie.xsl
$Revision: 4 $
Outputs a pie chart.

Created by: Virgil Dupras
Created on: 2004/09/16

Last modified by:$Author: hardcoded.software $
Last modified on:$Date: 2006-08-26 14:05:46 +0000 (Sat, 26 Aug 2006) $

Copyright (C) 2004-2006 Virgil Dupras (http://www.hardcoded.net)

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-->
<xsl:stylesheet version="1.1"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:math="http://exslt.org/math"
	extension-element-prefixes="math"
	xmlns="http://www.w3.org/2000/svg"
	exclude-result-prefixes="#default">

<xsl:script implements-prefix="math"
      language="java"
      src="java:java.lang.Math" />


<xsl:output
	method="xml"
	doctype-public="-//W3C//DTD SVG 1.0//EN"
	doctype-system="http://www.w3.org/TR/SVG/DTD/svg10.dtd"
	indent="yes"/>

<xsl:template name="draw_pie"> <!-- This is called when the main script processes the 'series' element -->
	<xsl:if test="function-available('math:sin') and function-available('math:sqrt')">
		<g style="stroke:black;stroke-width:1" transform="translate(350,150)">
			<xsl:variable name="total_y" select="sum(serie/point/@y)"/>
			<xsl:variable name="pi" select="3.141592"/>
			<xsl:variable name="radius">
				<xsl:choose>
					<xsl:when test="serie/@explode">130</xsl:when>
					<xsl:otherwise>149</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:for-each select="serie">
				<xsl:variable name="curr_y_tot" select="sum(preceding-sibling::*/point/@y)"/>
				<xsl:variable name="curr_y" select="sum(point/@y)"/>
				<xsl:variable name="angle_a" select="($curr_y div $total_y) * $pi * 2"/>
				<xsl:variable name="angle_a_text" select="$angle_a div 2"/>
				<xsl:variable name="sin_a" select="math:sin($angle_a)"/>
				<xsl:variable name="cos_a" select="math:cos($angle_a)"/>
				<xsl:variable name="sin_a_text" select="math:sin($angle_a_text)"/>
				<xsl:variable name="cos_a_text" select="math:cos($angle_a_text)"/>
				<xsl:variable name="large_arc">
					<xsl:choose>
						<xsl:when test="$angle_a >= $pi">1</xsl:when>
						<xsl:otherwise>0</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<!-- Sin A/a = Sin B/b = Sin C/c -->
				<!-- A = $myangle B = (180 - $myangle)/2 C = B . b = 150 c = 150. h seperates the triangle in 2 right triangles. our y pos will be h, and out x pos will be the left part of c -->
				<!-- c Sin A = h. Thus 150 Sin $myangle = h = ypos -->
				<!-- IMPORTANT NOTE: math:sin works in radian -->
				<xsl:variable name="ypos" select="-1 * $radius * $sin_a"/>
				<xsl:variable name="xpos" select="$radius * $cos_a"/>
				<xsl:variable name="ypos_text" select="(-1 * ($radius div 2) * $sin_a_text)"/>
				<xsl:variable name="xpos_text" select="($radius div 2) * $cos_a_text"/>
				<g style="fill:{color}">
					<xsl:attribute name="transform">
						<xsl:text>rotate(-</xsl:text><xsl:value-of select="($curr_y_tot div $total_y) * 360"/><xsl:text>) </xsl:text>
						<xsl:if test="@explode">translate(<xsl:value-of select="round($xpos_text div 4)"/>,<xsl:value-of select="round($ypos_text div 4)"/>) </xsl:if>
					</xsl:attribute>
					<path d="M 0 0 h {$radius} A {$radius},{$radius} 0,{$large_arc},0 {$xpos},{$ypos} z"/>
					<xsl:if test="../@showvalues">
						<text class="values" x="{round($xpos_text)}" y="{round($ypos_text) + 6}" transform="rotate({($curr_y_tot div $total_y) * 360} {round($xpos_text)} {round($ypos_text)})">
							<xsl:if test="@valuecolor">
								<xsl:attribute name="style">fill:<xsl:value-of select="@valuecolor"/></xsl:attribute>
							</xsl:if>
							<xsl:value-of select="round((sum(point/@y) div $total_y) * 100)"/>%
						</text>
					</xsl:if>
				</g>
			</xsl:for-each>
		</g>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
